home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / AmigaTalk / general / List.st < prev    next >
Text File  |  2000-02-13  |  2KB  |  107 lines

  1. "-------------------------------------------------------"
  2. "  Lists are implemented using Points in order to       "
  3. "  reduce the number of classes in the standard prelude "
  4. "-------------------------------------------------------"
  5.  
  6. Class List :SequenceableCollection
  7. ! first current !
  8. [
  9.    add: anItem
  10.       first <- (Point new x: anItem ) y: first.
  11.       ^ anItem
  12. |
  13.    addFirst: anItem
  14.       first <- (Point new x: anItem ) y: first.
  15.       ^ anItem
  16. |
  17.    addLast: anItem
  18.       (first isNil) 
  19.          ifTrue: [^ self addFirst: anItem].
  20.  
  21.       (self findLast) y: ((Point new x: anItem) y: nil).
  22.  
  23.       ^ anItem
  24. |
  25.    addAllFirst: aCollection
  26.       aCollection do: [:x | self addFirst: x]
  27. |   
  28.    addAllLast: aCollection
  29.       aCollection do: [:x | self addLast: x]
  30. |
  31.    coerce: aCollection    ! newList !
  32.       newList <- List new.
  33.  
  34.       aCollection do: [:x | newList addLast: x].
  35.  
  36.       ^ newList
  37. |
  38.    findLast     ! item !
  39.       ((item <- first) isNil)
  40.          ifTrue: [^ nil].
  41.  
  42.       [(item y) notNil]
  43.          whileTrue: [item <- item y].
  44.  
  45.       ^ item
  46. |
  47.    remove: anItem
  48.       ^ self remove: anItem 
  49.            ifAbsent: [self error: 'cant find item']
  50. |
  51.    remove: anItem ifAbsent: exceptionBlock
  52.       (first isNil) 
  53.          ifTrue: [^ exceptionBlock value].
  54.  
  55.       self inject: nil into: [:prev :current |
  56.  
  57.          (current x == anItem)
  58.             ifTrue: [(prev isNil)
  59.                      ifTrue: [first <- current y]
  60.                      ifFalse: [prev y: (current y)].
  61.          
  62.                 ^ anItem].
  63.       
  64.          current ] .
  65.       
  66.       ^ exceptionBlock value
  67. |
  68.    removeError
  69.       ^ self error: 'cannot remove from an empty list'
  70. |
  71.    removeFirst      ! item !
  72.       (first isNil)
  73.          ifTrue: [^ self removeError].
  74.  
  75.       item  <- first.
  76.       first <- first y.
  77.  
  78.       ^ item x
  79. |
  80.    removeLast
  81.       (first isNil)
  82.          ifTrue: [^ self removeError].
  83.  
  84.       ^ self remove: self last 
  85.            ifAbsent: [self removeError]
  86. |
  87.    first
  88.       ^ ((current <- first) notNil) 
  89.          ifTrue: [ current x ]
  90. |
  91.    next
  92.       ^ ((current <- current y) notNil)
  93.          ifTrue: [ current x ]
  94. |
  95.    current
  96.       ^ current x
  97. |
  98.    last
  99.       (first isNil) 
  100.          ifTrue: [^ nil].
  101.   
  102.       ^ self findLast x
  103. |
  104.    isEmpty
  105.       ^ first == nil
  106. ]
  107.